package com.daxidi.cn.service;

import com.daxidi.cn.config.wxconfig.WxPayConfig;
import com.daxidi.cn.utils.HttpClientUtils;
import com.daxidi.cn.utils.UuidUtils;
import com.daxidi.cn.utils.WxPayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;
import java.util.Random;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * @author jialin
 * @createTime 2020-09-28
 */
@Service
public class WxPayService {

    @Autowired
    private WxPayConfig wxPayConfig;

    @Autowired
    private IShopService shopService;

    public Map unifiedOrderJs(Integer totalFee, String body, String outTradeNo, String openId) throws Exception {
        //生成有序map
        SortedMap<String, String> param = new TreeMap<>();
        //公众账号ID
        param.put("appid", wxPayConfig.getAppid());
        //商品描述
        param.put("body", body);
        //商户号
        param.put("mch_id", wxPayConfig.getMchid());
        //随机字符串
        param.put("nonce_str", UuidUtils.createNonceStr());
        //通知地址，异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。
        param.put("notify_url", wxPayConfig.getNotifyUrl());
        //商户订单号
        param.put("out_trade_no", outTradeNo);
        //终端IP，支持IPV4和IPV6两种格式的IP地址。用户的客户端IP
        param.put("spbill_create_ip", "192.168.3.97");
        //会员小程序ID
        param.put("sub_appid", "wx19b5a41593f4211b");
        //子商户号
        param.put("sub_mch_id", shopService.getSubMchId(Long.valueOf(outTradeNo)));
        //openId
        param.put("openid", openId);
        //标价金额，单位分
        param.put("total_fee", totalFee + "");
        //交易类型，这里是二维码支付
        param.put("trade_type", "JSAPI");
        //param.put("product_id", productId);
        //商品ID，trade_type=NATIVE时，此参数必传。此参数为二维码中包含的商品ID，商户自行定义。
        //签名
        param.put("sign", WxPayUtils.generateSignature(param, wxPayConfig.getKey()));
        //param.put("sign", WxPayUtils.generateSignature(param, "YrBTPVTMUhaevioNJDXD1r6v0QNXmABN"));//签名
        String paramXML = WxPayUtils.mapToXml(param);
        String result = HttpClientUtils.doPost(wxPayConfig.getUnifiedUrl(), paramXML, 5000);
        Map<String, String> mapResult = WxPayUtils.xmlToMap(result);
        String prepayId = mapResult.get("prepay_id");
        if (StringUtils.isEmpty(prepayId)) {
            throw new RuntimeException("error");
        }
        return mapResult;
    }

    public Map unifiedOrderMcro(Integer totalFee, String body, String outTradeNo, String openId) throws Exception {
        //生成有序map
        SortedMap<String, String> param = new TreeMap<>();
        //公众账号ID
        param.put("appid", wxPayConfig.getAppid());
        //商品描述
        param.put("body", body);
        //商户号
        param.put("mch_id", wxPayConfig.getMchid());
        //随机字符串
        param.put("nonce_str", UuidUtils.createNonceStr());
        //通知地址，异步接收微信支付结果通知的回调地址，通知url必须为外网可访问的url，不能携带参数。
        param.put("notify_url", wxPayConfig.getNotifyUrls());
        //商户订单号
        param.put("out_trade_no", outTradeNo);
        //终端IP，支持IPV4和IPV6两种格式的IP地址。用户的客户端IP
        param.put("spbill_create_ip", "192.168.3.97");
        //会员小程序ID
        param.put("sub_appid", "wx19b5a41593f4211b");
        //子商户号
        param.put("sub_mch_id", shopService.getSubMchId(Long.valueOf(outTradeNo)));
        //openId
        param.put("openid", openId);
        //标价金额，单位分
        param.put("total_fee", totalFee + "");
        //交易类型，这里是二维码支付
        param.put("trade_type", "JSAPI");
        //param.put("product_id", productId);//商品ID，trade_type=NATIVE时，此参数必传。此参数为二维码中包含的商品ID，商户自行定义。
        //签名
        param.put("sign", WxPayUtils.generateSignature(param, wxPayConfig.getKey()));
        //签名
        //param.put("sign", WxPayUtils.generateSignature(param, "YrBTPVTMUhaevioNJDXD1r6v0QNXmABN"));
        String paramXML = WxPayUtils.mapToXml(param);
        String result = HttpClientUtils.doPost(wxPayConfig.getUnifiedUrl(), paramXML, 5000);
        Map<String, String> mapResult = WxPayUtils.xmlToMap(result);
        String prepayId = mapResult.get("prepay_id");
        if (StringUtils.isEmpty(prepayId)) {
            throw new RuntimeException("error");
        }
        return mapResult;
    }


    public String unifiedOrder() throws Exception {
        //生成有序map
        SortedMap<String, String> param = new TreeMap<>();
        param.put("appid", "wxab8acb865bb1637e");
        param.put("mch_id", "11473623");
        param.put("nonce_str", UuidUtils.createNonceStr());
        param.put("body", "星空");
        int num = Math.abs(new Random().nextInt());
        param.put("out_trade_no", "1615361392329");
        param.put("total_fee", "1");
        param.put("spbill_create_ip", "192.168.160.1");
        param.put("notify_url", wxPayConfig.getNotifyUrl());
        param.put("trade_type", "NATIVE");
        param.put("product_id", "202161015");
        param.put("sign", WxPayUtils.generateSignature(param, "2ab9071b06b9f739b950ddb41db2690d"));//签名
        String paramXML = WxPayUtils.mapToXml(param);
        String result = HttpClientUtils.doPost(wxPayConfig.getUnifiedUrl(), paramXML, 5000);
        Map<String, String> mapResult = WxPayUtils.xmlToMap(result);
        String codeUrl = mapResult.get("code_url");
        if (StringUtils.isEmpty(codeUrl)) {
            throw new RuntimeException("error");
        }
        return codeUrl;
    }
}
